package com.msmall.community.klg.dao.hibernate;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.klg.dao.ArticleDao;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.model.SegmentArticle;

public class ArticleDaoHibernate extends BaseDaoHibernate<Article,Long> implements ArticleDao{

	public ArticleDaoHibernate(){
		super(Article.class);
	}
	/**
	 * 通过文章ID 查找文章分类
	 * @param articleid
	 * @return
	 * @throws Exception
	 */
	public SegmentArticle getSegmentByArticleid(Long articleid) throws Exception{
		try{
			String queryString = "from SegmentArticle p where p.articleid=?";
			List list = super.getHibernateTemplate().find(queryString, articleid);
			if(list != null && list.size() > 0)
				return (SegmentArticle) list.get(0);
			return null;
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 通过文章ID 查找文章以及文章分类<br/>
	 * object[0] = Article Object[1]=SegmentArticle
	 * @param articleid
	 */
	public Object[] getArticleAndSegmentByArticle(Long articleid) throws Exception{
		try{
			String queryString = "SELECT a, p from Article a, SegmentArticle p where p.articleid=? AND a.articleid=?";
			List list = super.getHibernateTemplate().find(queryString, new Object[]{articleid, articleid});
			if(list != null && list.size() > 0)
				return (Object[]) list.get(0);
			return null;
		}catch(Exception e){
			throw e;
		}
	}
}
